package com.kude.service;

import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kude.entity.ArticleCategory;
import com.kude.entity.ProductCategory;
import com.kude.manager.ArticleCategoryManager;
import com.kude.manager.OperException;
import com.kude.util.BeanFactory;

public class ArticleCategoryService {
	private static final Logger logger = LoggerFactory
			.getLogger(ArticleCategoryService.class.getName());

	private ArticleCategoryManager articleCategoryManager;

	/**
	 * 获取指定ID的类别
	 * 
	 * @param id
	 * @return
	 * @throws OperException
	 */
	public ArticleCategory getArticleCategory(int id) throws OperException {
		return articleCategoryManager.get(id);
	}

	/**
	 * 增加文章类别
	 * 
	 * @param articleCategory
	 * @return
	 * @throws OperException
	 */
	public boolean addArticleCategory(ArticleCategory articleCategory)
			throws OperException {
		// TODO 保证Prefix的唯一
		articleCategoryManager.add(articleCategory);
		return true;

	}

	/**
	 * 更新文章类别
	 * 
	 * @param articleCategory
	 * @throws OperException
	 */
	public void updateArticleCategory(ArticleCategory articleCategory)
			throws OperException {
		articleCategoryManager.update(articleCategory);
	}

	/**
	 * 删除文章类别
	 * 
	 * @param id
	 * @throws OperException
	 */
	public void deleteArticleCategory(int id) throws OperException {
		articleCategoryManager.delete(id);
	}

	/**
	 * 根据类别前缀获取文章的类别ID集合
	 * @param articleManager
	 * @throws OperException 
	 */
	public List<ArticleCategory> getArticleCategoryListByPrefix(String prefix) throws OperException{
		SimpleExpression res = Restrictions.like("prefix", prefix+"%");
		return articleCategoryManager.find(res);
	}
	
	/**
	 * 通过parentID来获取属于同一大类别的小类别
	 * @param parentId
	 * @return
	 * @throws OperException
	 */
	public List<ArticleCategory> getArticleCategoryListByParentId(int parentId) throws OperException{
		SimpleExpression res = Restrictions.eq("articleCategoryPrefix.prefixId",parentId);
		return articleCategoryManager.find(res);
	}

	public ArticleCategoryManager getArticleCategoryManager() {
		return articleCategoryManager;
	}

	public void setArticleCategoryManager(
			ArticleCategoryManager articleCategoryManager) {
		this.articleCategoryManager = articleCategoryManager;
	}


	

}
